import json

from flask import Blueprint,jsonify

from utils.db import db1
from utils.redisconn import r

bp_workflow = Blueprint('workflow',__name__)


@bp_workflow.route('/worklist')
def worklist():
    sql = "select * from workflow"
    res = db1.find_all(sql)
    return jsonify({'code':200,'list':res})


# 获取数据源接口
@bp_workflow.route('/getresourcedata')
def getresourcedata():
    #从redis取，如果有直接返回
    values = r.get_str('dataresouce')
    if values:
        pass
        list = eval(values)
    else:
        #如果没有查询
        sql = "select * from data_source"
        res = db1.find_all(sql)
        list = []
        if res:
            for i in res:
                dict = {'id':i['id'],'name':i['name']}
                sql1 = "select id,name,type from %s"%(i['tablename'])
                subres = db1.find_all(sql1)
                dict['mes'] = subres
                list.append(dict)
        #存入redis中
        r.set_str('dataresouce',json.dumps(list))
    return jsonify({'code':200,'list':list})

